package com.zmm.myyimixian.ui.activity.main.home;

import android.os.Handler;
import android.os.Message;

import com.zmm.myyimixian.bean.HomeBoosTitle;
import com.zmm.myyimixian.config.ConfigPath;
import com.zmm.myyimixian.http.OkHttpClientUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wencai2015 on 2016/10/21.
 */

public class MyHomeBoosTitleThread extends Thread {
    private Handler handler;
    private String id;
    public MyHomeBoosTitleThread(Handler handler,String id){
        this.handler = handler;
        this.id = id;
    }
    @Override
    public void run() {
        try {
            String fromUrl = OkHttpClientUtils.getStringFromUrl(ConfigPath.getHomeBoos(id));
            JSONObject jsonObject = new JSONObject(fromUrl);
            JSONArray specials = jsonObject.getJSONArray("specials");
            List<HomeBoosTitle> list = new ArrayList<>();
            for (int i = 0; i < specials.length(); i++) {
                JSONObject object = specials.getJSONObject(i);
                String id = object.getString("id");
                String name = object.getString("name");
                String subtitle = object.getString("subtitle");
                list.add(new HomeBoosTitle(id, name, subtitle));
            }
            Message msg = Message.obtain();
            msg.what = 1;
            msg.obj = list;
            handler.sendMessage(msg);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
